from collections import defaultdict

n = int(input())
s = input().split()
t = input().split()

s_nums = defaultdict(lambda: [0])
t_nums = defaultdict(lambda: [0])

for i in range(n):
    for num in (2, 4, 0):
        if str(num) in s[i]:
            s_nums[num].append(i)
        if str(num) in t[i]:
            t_nums[num].append(i)
for num in (0, 2, 4):
    s_nums[num].append(n)
    t_nums[num].append(n)

# 在最后一位增加 240 虚拟节点
dp = [[0 for _ in range(2)] for _ in range(n + 1)]  # 以i开始得到的最大长度 |0 为s序列 1 为 t序列

dp[0][1] = 1
max_val = 1


def find(array, target):
    l = 0
    r = len(array) - 1
    while l <= r:
        mid = (l + r) >> 1
        if array[mid] < target:
            l = mid + 1
        else:
            r = mid - 1
    return l - 1


for i in range(1, n):
    for num in (0, 2, 4):
        # 去找到下一个能够到达的位置
        if str(num) in t[i]:
            next_index = find(s_nums[num], i)
            if next_index >= 0:
                next_index = s_nums[num][next_index]
                dp[i][1] = max(dp[i][1], dp[next_index][0] + 1)
        if str(num) in s[i]:
            next_index = find(t_nums[num], i)
            if next_index >= 0:
                next_index = t_nums[num][next_index]
                dp[i][0] = max(dp[i][0], dp[next_index][1] + 1)
    max_val = max(max_val, max(dp[i]))
print(max_val - 1)
